package com.meadvc.ssmdemo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meadvc.ssmdemo.dao.ComponentDao;
import com.meadvc.ssmdemo.service.OrderService;
import com.meadvc.ssmdemo.util.BT_Algorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: ssm-demo
 * @description:
 * @author: meadvc
 * @create: 2020-06-18 22:56
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private ComponentDao componentDao;
    @Override
    public JSONObject getOrder(JSONObject requestJson) {
        System.out.println(requestJson);
        List<JSONObject> querydata =(List<JSONObject>) requestJson.get("data");
        int index = querydata.size();
        JSONObject resultJson = new JSONObject();
        List<JSONObject> list = new ArrayList<JSONObject>();
        JSONObject meta = new JSONObject();
        for(int i = 0;i<index;i++){
            JSONObject json = new JSONObject();

            JSONObject js = new JSONObject(querydata.get(i));
            String cname = js.getString("cname");
            int num = js.getIntValue("num");
            if(num!=0){
                List<JSONObject> li = componentDao.getCompByName2(cname);
                json.put("cname",cname);
                json.put("data",li);
                json.put("num",num);
                //System.out.println(json);
                list.add(json);
            }
        }
        //调用算法
        JSONObject best = (JSONObject) BT_Algorithm.getBest(list);

        if(best==null||best.isEmpty()){
            meta.put("msg","表单获取失败");
            meta.put("status",400);
        }else{
            meta.put("msg","表单获取成功");
            meta.put("status",200);
        }

        resultJson.put("best",best);
        //resultJson.put("data",list);
        resultJson.put("meta",meta);
        return resultJson;
    }

    @Override
    public JSONObject updateOrder(JSONObject requestJson) {
        JSONObject resultJson = new JSONObject();
        JSONObject meta = new JSONObject();

        //System.out.println(requestJson.get("data"));
        JSONObject best = new JSONObject((Map<String, Object>) requestJson.get("data"));
        BigDecimal best_price = BigDecimal.valueOf((Double) best.get("best_price"));
        //System.out.println(best_price);
        List<JSONObject> blist = (List<JSONObject>) best.get("bestlist");

        //JSONObject update = (JSONObject) requestJson.get("update");
        JSONObject update = new JSONObject((Map<String, Object>) requestJson.get("update"));

        String cname = (String) update.get("cname");
        String sname = (String) update.get("sname");
        List<JSONObject> ulist = new ArrayList<>();
        ulist = (List<JSONObject>) update.get("comps");
        int index1 = ulist.size();
        int index2 = blist.size();
        for(int i = 0; i < index1; i++){
            JSONObject json = new JSONObject(ulist.get(i));
            if(sname.equals((String)json.get("sname"))) {
                for(int j = 0 ;j < index2;j++){
                    JSONObject jsonObject = new JSONObject(blist.get(j));
                    if(cname.equals((String)jsonObject.get("cname"))){
                        jsonObject.put("price",json.get("price"));
                        jsonObject.put("cid",json.get("cid"));
                        jsonObject.put("sname",json.get("sname"));

                        BigDecimal price = BigDecimal.valueOf((Double) json.get("price"));
                        price = price.multiply(BigDecimal.valueOf(jsonObject.getIntValue("num")));
                        best_price = best_price.subtract(BigDecimal.valueOf((Double) jsonObject.get("t_price")));
                        //System.out.println(best_price);
                        best_price = best_price.add(price);
                        //System.out.println(best_price);
                        jsonObject.put("t_price",price);
                        best.put("best_price",best_price);
                        //消息
                        meta.put("msg","表单更新成功");
                        meta.put("status",200);
                    }
                }
            }
        }
        resultJson.put("data",best);
        //resultJson.put("data",list);
        resultJson.put("meta",meta);
        return resultJson;
    }

    /**
     * 根据cname删除订单中的零件
     * @param requestJson
     * @return
     */
    @Override
    public JSONObject deleteComp(JSONObject requestJson) {
        JSONObject resultJson = new JSONObject();
        JSONObject meta = new JSONObject();

        //要删除的零件种类
        String cname = (String) requestJson.get("cname");

        //订单数据
        JSONObject best = new JSONObject((Map<String, Object>) requestJson.get("data"));
        BigDecimal best_price = BigDecimal.valueOf((Double) best.get("best_price"));
        List<JSONObject> blist = (List<JSONObject>) best.get("bestlist");

        int index = blist.size();
        for(int i = 0; i < index ; i++) {

            JSONObject json = new JSONObject(blist.get(i));
            if(cname.equals(json.get("cname"))) {
                best_price = best_price.subtract(BigDecimal.valueOf((Double) json.get("t_price")));
                best.put("best_price",best_price);
                blist.remove(json);
                //消息
                meta.put("msg","零件删除成功");
                meta.put("status",200);
            }
        }

        resultJson.put("data",best);
        //resultJson.put("data",list);
        resultJson.put("meta",meta);
        return resultJson;
    }
}
